﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using EnvDTE;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

namespace SPTotalAutomationToolkit.SP2013IntranetProjectTemplate.Wizard
{
    internal class ProjectWizardModel
    {
        private DTE dteObject;
        private ISharePointProjectService projectServiceValue;
        private List<string> validatedUrls = new List<string>();

        internal ProjectWizardModel(DTE dteObject, bool requiresFarmPriveleges)
        {
            this.dteObject = dteObject;
            
            // Initialize default values for wizard choices.
            CurrentPage = 1;
            IsSandboxed = !requiresFarmPriveleges;
            UseHostNamedSiteCollections = true;
            IsSecondPagePopulated = false;
        }

        #region Properties shared by the wizard implementation and the wizard UI

        internal ISharePointProjectService ProjectService
        {
            get
            {
                if (projectServiceValue == null)
                {
                    projectServiceValue = GetProjectService();
                }
                return projectServiceValue;
            }
        }

        internal int CurrentPage { get; set; }

        internal string ClientName { get; set; }
        internal string ClientNameWithoutSpaces
        {
            get
            {
                return ClientName.Replace(" ", "");
            }
        }
        internal string ProjectName { get; set; }
        internal string ProjectNameWithoutSpaces
        {
            get
            {
                return ProjectName.Replace(" ", "");
            }
        }
        internal bool IsSecondPagePopulated { get; set; }
        internal bool IsSandboxed { get; set; }
        internal bool UseHostNamedSiteCollections { get; set; }
        internal string IntranetWebAppHostHeaderForDev { get; set; }
        internal string IntranetWebAppPortForDev { get; set; }
        internal bool WriteIntranetHostFileEntry { get; set; }
        internal bool WriteServiceHubHostFileEntry { get; set; }
        internal string PeopleWebAppHostHeaderForDev { get; set; }
        internal string PeopleWebAppPortForDev { get; set; }
        internal bool WritePeopleHostFileEntry { get; set; }
        internal string SiteUrlForDebugging { get; set; }
        internal string ServerNameForDev { get; set; }
        internal string ServerNameForDevWithFQDN { get; set; }
        internal string ServerPortForDev { get; set; }
        #endregion

        #region Private methods

        private string GetLocalHostUrl()
        {
            const string HttpScheme = "http";
            UriBuilder builder = new UriBuilder(HttpScheme, Environment.MachineName.ToLowerInvariant());
            return builder.ToString();
        }

        private ISharePointProjectService GetProjectService()
        {
            ServiceProvider serviceProvider = new ServiceProvider(dteObject as IOleServiceProvider);
            return serviceProvider.GetService(typeof(ISharePointProjectService)) as ISharePointProjectService;
        }

        private IVsThreadedWaitDialog2 ShowProgressDialog(string caption, string message)
        {
            IOleServiceProvider oleServiceProvider = dteObject as IOleServiceProvider;
            IVsThreadedWaitDialogFactory dialogFactory = new ServiceProvider(oleServiceProvider).GetService(
                typeof(SVsThreadedWaitDialogFactory)) as IVsThreadedWaitDialogFactory;

            if (dialogFactory == null)
            {
                throw new InvalidOperationException("The IVsThreadedWaitDialogFactory object could not be retrieved.");
            }

            IVsThreadedWaitDialog2 vsThreadedWaitDialog = null;
            ErrorHandler.ThrowOnFailure(dialogFactory.CreateInstance(out vsThreadedWaitDialog));
            ErrorHandler.ThrowOnFailure(vsThreadedWaitDialog.StartWaitDialog(caption, message,
                 null, null, String.Empty, 0, false, true));
            return vsThreadedWaitDialog;
        }

        private void CloseProgressDialog(IVsThreadedWaitDialog2 vsThreadedWaitDialog)
        {
            if (vsThreadedWaitDialog == null)
            {
                throw new ArgumentNullException("vsThreadedWaitDialog");
            }

            int canceled;
            ErrorHandler.ThrowOnFailure(vsThreadedWaitDialog.EndWaitDialog(out canceled));
        }
        #endregion
    }
}